package mess;

import java.util.Arrays;

public class LeeCode_80 {
    public static void main(String[] args) {
        System.out.println(removeDuplicates2(new int[]{1, 1, 1, 2, 3}));
    }
    private static int removeDuplicates(int[] nums) {
        int len = nums.length;
        int temp = nums[0];
        int count = 1;
        int result = 1;
        for(int i = 1; i < len; i++){
            if(temp == nums[i]){
                count++;
                if(count > 2){
                    nums[i] = 10000;
                    continue;
                }
            }else{
                temp = nums[i];
                count = 1;
            }
            result++;
        }
        Arrays.sort(nums);
        return result;
    }
    private static int removeDuplicates2(int[] nums) {
        int i = 0;
        for (int n : nums)
            if (i < 2 || n > nums[i-2])
                nums[i++] = n;
        return i;
    }
}
